package reflect;
import java.lang.String;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * java反射机制
 *
 */
public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException {
        //获取String的类对象
        //Class cls = String.class;
        //Class cls = ArrayList.class;

        //Class cls = Class.forName("java.lang.String");
        //Class cls = Class.forName("java.util.ArrayList");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个类");
        String className = scanner.nextLine();
        Class cls = Class.forName(className);

        //通过类对象反映出其表示的类的完全限定名
        String name = cls.getName();
        System.out.println(name);
        name  = cls.getSimpleName();
        System.out.println(name);

        Package pack = cls.getPackage();
        System.out.println(pack.getName());

        Method[] methods = cls.getMethods();
        System.out.println("共："+methods.length+"个方法");
        for(Method method:methods){
            System.out.println(method.getName());
        }

        System.out.println("-----------------------");
        //Class c1 = Person.class;
        //System.out.println(c1);
        //Class c1 = Class.forName("java.util.ArrayList");
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入一个类");
        Class c1 = Class.forName(scanner1.nextLine());
        System.out.println(c1.getName());
        System.out.println(c1.getPackage());
        System.out.println(c1.getSimpleName());

        Method[] methods1 = c1.getMethods();
        System.out.println("共"+methods1.length+"个方法");
        for(Method method : methods1){
            System.out.println(method.getName());
        }
    }

}

